package com.xpgk.mvc.statistic.monthTotalStatistic;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.tools.ToolDateTime;

public class MonthTotalStatisticController  extends  BaseController{
	
	private  MonthTotalStatisticServices  monthTotalStatisticServices;
	
	public  void  index()
	{	
		String organids = getCUserOrganIds();
		Organization  organ = Organization.dao.findById(organids);
		String year = ToolDateTime.format(new Date(), ToolDateTime.pattern_y);
		setAttr("organization", organ);
		setAttr("year", year);
		render("monthTotalStatistic.html");
	}
	@Before({Tx.class })
	public  void  getMonthTotal() throws ParseException
	{
		String year = getPara("year");
		String organids = getPara("organIds");
		String sql  = getSqlMy(MonthTotal.sqlId_getmonthTotalList);
		String dnurse = "SELECT COUNT(ids) as fwrs,SUM(gjWeight) as sjjzjs FROM sb_customer_service_nurse WHERE YearMon = ? and organids = ?";
		String yjjsSql = "SELECT SUM(delWeight) as yjjs from sb_customer_detail WHERE  DATE_FORMAT(firstDate, '%Y-%m') = ? AND organIds = ? and dataStatus in ('1','2')";
		String xgkSql = "SELECT customerIds FROM sb_customer_detail WHERE  DATE_FORMAT(firstDate, '%Y') = ? AND organIds = ? and dataStatus in ('1','2')";
		String sgksjjsSql = "SELECT gjWeight from sb_customer_service_nurse WHERE customerID = ? and YearMon = ?";
		String sjjsSql = "SELECT SUM(gjWeight) as sjjs from sb_customer_service_nurse WHERE organids = ? and YearMon = ?";
		List<Record>  dntList =  Db.find(sql, organids,year);
		double yjjsSum = 0d;
		double sjjsSum = 0d;
		for(Record dnt:dntList){
			
			int month = dnt.getInt("date");
			String monthStr = month+"";
			if(month<10){
				monthStr = "0"+monthStr;
			}
			String yearMonth = year+"-"+monthStr;//2017-04
			//添加服务人数、实际减重斤数
			Record record = Db.findFirst(dnurse,yearMonth,organids);
			if(record!=null){
				String fwrs = record.getLong("fwrs")+"";
				if(fwrs!=null && !fwrs.equals("")){
					dnt.set("fwrs", fwrs);
				}
				if(record.getDouble("sjjzjs")!=null ){
					dnt.set("sjjzjs", new DecimalFormat("#.0").format( record.getDouble("sjjzjs")) );
				}
			}
			
			//添加当年当月新增顾客应减斤数
			Record yjjsRecord = Db.findFirst(yjjsSql,yearMonth,organids);
			double yjjs = 0d;
			if(yjjsRecord.getDouble("yjjs")!=null ){
				dnt.set("xgkyjjs", yjjsRecord.getDouble("yjjs"));
				yjjs =yjjsRecord.getDouble("yjjs");
				yjjsSum = yjjsSum +yjjs;
			}
			//添加当年当月新增顾客实减斤数
			List<Record> xgkRecord = Db.find(xgkSql,year,organids);
			double sjjs = 0d;
			if(xgkRecord!=null && xgkRecord.size()>0){
				for (Record xgk:xgkRecord) {
					String customerIds = xgk.getStr("customerIds");
					Record gjRecord = Db.findFirst(sgksjjsSql,customerIds,yearMonth);
					if(gjRecord!=null ){
						if(gjRecord.getFloat("gjWeight")!=null ){
							sjjs = sjjs+gjRecord.getFloat("gjWeight");
							
						}
						
					}
				}
				sjjsSum = sjjsSum + sjjs;
				dnt.set("dndyxgksjjs", new DecimalFormat("0.0").format( sjjs) );
			}
			
			//添加实减斤数
			Record sjjsRecord = Db.findFirst(sjjsSql,organids,yearMonth);
			if(sjjsRecord!=null && sjjsRecord.getDouble("sjjs")!=null){
				dnt.set("sjjzjs", sjjsRecord.getDouble("sjjs"));
			}
			//添加累计减重率
			if(yjjsSum!=0){
				dnt.set("ljjzl", (sjjsSum/yjjsSum)*100);
			}
			
		}
		
		//		List<MonthTotal>  dntList =  MonthTotal.dao.find(getSqlMy(MonthTotal.sqlId_getMonthTotal), organids,"%"+year+"%",year);
		
		//添加旧数据到月综合查询(勿删)
//		monthTotalStatisticServices.saveOld();
		
		//添加旧数据到年综合查询(勿删)
	//	monthTotalStatisticServices.saveYearOld();
		
		
		//添加掉秤人数，涨称人数，平称人数，掉秤比例，使用产品人数，使用产品占比 
		//到日，月，年，综合查询(勿删)
//		monthTotalStatisticServices.saveRynTotal();
		
		
		renderJson(dntList);
	}

}
